package com.exchange.used.service.impl;

import com.exchange.used.config.converters.LayuiResult;
import com.exchange.used.entity.User;
import com.exchange.used.repository.UserRepository;
import com.exchange.used.service.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("UserService")
public class UserService implements UserServiceI {

    @Autowired
    UserRepository userRepository;

    @Override
    @Transactional
    public LayuiResult doLogin(String username, String password) {
        LayuiResult result = new LayuiResult();
        try {
            if(username == null || username.isEmpty()){
                result.setCode(-1);
                result.setMsg("用户名不能为空！");
                return result;
            }
            if(password == null || password.isEmpty()){
                result.setCode(-1);
                result.setMsg("密码不能为空！");
                return result;
            }
            User user = userRepository.getUserByUsernameAndPassword(username, password);
            if(user == null){
                result.setCode(-1);
                result.setMsg("用户名/密码 错误！");
                return result;
            }else{
                result.setMsg("用户[" + user.getUsername() + "]登录成功");
            }
            result.getData().put("user", user);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(-1);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    @Transactional
    public LayuiResult changePassword(String userid, String oldPassword, String newPassword, String repeatPassword) {
        LayuiResult result = new LayuiResult();
        try {
            if(!userRepository.getOne(userid).getPassword().equalsIgnoreCase(oldPassword)){
                result.setCode(-1);
                result.setMsg("原密码错误");
                return result;
            }
            if(!newPassword.equalsIgnoreCase(repeatPassword)){
                result.setCode(-1);
                result.setMsg("两次密码输入不一致");
                return result;
            }
            userRepository.updateUserPassword(userid, newPassword);
            result.getData().put("user", userRepository.getUserByUserid(userid));
            result.setMsg("密码更新成功");
        }catch (Exception e){
            result.setCode(-1);
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @Override
    @Transactional
    public LayuiResult changeUserInfo(User user) {
        LayuiResult result = new LayuiResult();
        try {
            if(user.getUserid() == null || user.getUserid().isEmpty()){
                result.setCode(-1);
                result.setMsg("user id can not be null ！");
                return result;
            }
            if(user.getUsername() == null || user.getUsername().isEmpty()){
                result.setCode(-1);
                result.setMsg("用户名不能为空！");
                return result;
            }
            User tempUser = userRepository.getUserByUsername(user.getUsername());
            if(tempUser != null && !tempUser.getUserid().equalsIgnoreCase(user.getUserid())){
                result.setCode(-1);
                result.setMsg("用户名已被使用！");
                return result;
            }
            userRepository.save(user);
            result.getData().put("user", userRepository.getUserByUserid(user.getUserid()));
            result.setMsg("个人信息修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(-1);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    @Transactional
    public LayuiResult register(User user) {
        LayuiResult result = new LayuiResult();
        try {
            if(user.getPassword() == null || user.getPassword() == null){
                result.setCode(-1);
                result.setMsg("密码不能为空！");
                return result;
            }
            if(user.getUsername() == null || user.getPassword() == null){
                result.setCode(-1);
                result.setMsg("用户名不能为空！");
                return result;
            }
            if(userRepository.getUserByUsername(user.getUsername()) != null){
                result.setCode(-1);
                result.setMsg("用户名已被使用！");
                return result;
            }
            userRepository.save(user);
            result.setMsg("注册成功");
            result.getData().put("user", user);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(-1);
            result.setMsg(e.getMessage());
        }
        return result;
    }

}
